<template>
    <view>
		<view class="d-waikuang-10">
			<u-sticky offset-top="0" bgColor="#ffffff">
				<!--导航-->
				<u-tabs-d 
					:list="daohanglist"
					@click="click" 
					lineColor="#da7595"
					:activeStyle="{
							color: '#000000',
							fontWeight: 'bold',
							transform: 'scale(1.05)'
						}"
				></u-tabs-d>
			</u-sticky>
			<view class=" d-shang-10">
				<uni-forms
					:modelValue="formData" 
					label-position="top"
				>
					
					<view class="d-kuan d-shang-10">
						<view class="d-beijing-hui">
							<uni-easyinput 
							    @input="descInput($event)"
								type="textarea" 
								autoHeight 
								v-model="neirong" 
								placeholder="( *必须 ) 请输入内容" 
								placeholder-style="color:#b8b8b8;font-size:32rpx;text-align:center;"
								placeholder-class="place-holder"
								:inputBorder="false"
								:border="true"
								maxlength="-1"
							>
							</uni-easyinput>
						</view>
					</view>
					<view class="d-kuan d-shang-10">
						<view class="example-body">
							<u-upload
								:fileList="fileList1"
								@afterRead="afterRead"
								@delete="deletePic"
								name="1"
								multiple
								:maxCount="9"
							></u-upload>
						</view>
					</view>
				</uni-forms>
			</view>
		</view>
		<view class="goods-carts">
			<view class="d-waikuang-10">
				<view class="d-kuan d-beijing-bai d-yuanjiao-10 ">
					<view class="d-neikuang-zuoyou-6-shangxia-10">
			    		<u-button text="提 交" form-type="submit" @click="postTijiao" shape="circle" color="linear-gradient(to right, rgb(67, 185, 185), rgb(255, 142, 187))"></u-button>
					</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
import config from "../../utils/config";
import fabudata from "../../utils/configfabu";

var Api = require("../../utils/api.js");
var util = require("../../utils/util.js");

var wxApi = require("../../utils/wxApi.js");
var wxRequest = require("../../utils/wxRequest.js");

var pageCount = config.getPageCount;
var webSiteName = config.getWebsiteName;
var domain = config.getDomain;
var topNav = config.getIndexNav;
var banbenhao = config.getDuixinbanbenhao;
var fankuitu = fabudata.postDUIXIN_FANKUITU_ID;

export default {
  data() {
    return {
      daohanglist: [],
	  formData: {},
	  neirong:'',
	  fileList1: [],
	  uptu:[],
	  keycode:'',
	  member:[],
	  uid:''
    };
  },
	mounted() {
		
	},
  components: {

  },
  props: {},
  onPageScroll: function (e) {
  	  
  },
  onShareAppMessage: function () {
    
  },
  onPullDownRefresh: function () {
    
  },
  onReachBottom: function () {
    
  },
  onLoad: function (options) {
	  var duixin = this;
	  var keycode = uni.getStorageSync('keycode');
	  var member = uni.getStorageSync('member');
	  var uid = uni.getStorageSync('memberuid');
	  duixin.keycode = keycode;
	  duixin.member = member;
	  duixin.uid = uid;
	  if (keycode) {
	  	
	  } else {
	  	//否则跳转到登录页面
	  	uni.navigateTo({
	  		url: '/pages/login/login'
	  	});
	  	
	  }
    duixin.getDaohanglistData();
  },


  onShow() {
	
	
  },
    methods: {
		descInput(e){
			var duixin = this; 
		   let newStr = '<p>' + e + '</p>';
		   let content = newStr.replace(/\n/g,"</p><p>");
		   duixin.neirong = content;
		   //console.log(duixin.neirongpp)
		},
		//导航
		getDaohanglistData(){
			var duixin = this;
			var getFankuidaohangRequest = wxRequest.getRequest(Api.getFankuidaohang());
			var res;
			getFankuidaohangRequest.then(response => {
				res = response;
				if(res.data.code) {
					var lists = res.data.data;
					let list = []; 
					for (const i in lists) {
						list.push({
							name: lists[i].name,
							id: lists[i].id
						});
					}
					duixin.daohanglist = list;
					//console.log('list',list);
				} else {
		
				  uni.showModal({
				  	title: '请先登录',
				  	//content: '请先登录',
				  	confirmText: '前往登录',
				  	success: function(res) {
				  		if (res.confirm) {
				  			uni.redirectTo({
				  				url: '/pages/login/login'
				  			});
				  		}
				  	}
				  });
				  
				}
			})
		},
		//导航点击
		click(e){
			var duixin = this;
			console.log(e.id);
			duixin.catid = e.id;
		},
		//提交
		postTijiao(){

			var duixin = this;
			var catid = duixin.catid;
			if(catid > 0) {
				var neirong = duixin.neirong;
				if(!neirong){
					
					//console.log('catid',catid); 
					uni.showToast({
						icon: 'none',
						title: '内容不能为空',
						duration: 5000
					});
				} else {
					//提交内容
					//console.log('catid222',catid); 
					duixin.postTiezi(neirong,catid);
				}
			} else {
				uni.showToast({
					icon: 'none',
					title: '请选择栏目',
					duration: 5000
				});
			}
		},
		postTiezi(neirong,catid){
			var duixin = this;
			let img = '';
			var tuji =duixin.fileList1;
			tuji.forEach(item=>{
				img+= '&data[tuji][id][]='+item.id+'&data[tuji][title][]=tupian'
			})
			var datas = "is_ajax=1&data[content]="+neirong+"&catid="+catid+img;
			var postFankuiRequest = wxRequest.postRequest(Api.postFankui(duixin.uid,duixin.keycode),datas);
			postFankuiRequest.then(response => {
				if (response.data.code == 1) {
					
					duixin.neirong = '';
					duixin.catid = 0;
					uni.showModal({
						title: '提交成功，等待核对',
						//content: '请先登录',
						confirmText: '返回',
						success: function(res) {
							if (res.confirm) {
								uni.redirectTo({
									url: '/pages/fankui/fankui'
								});
							}
						}
					});
					//uni.reLaunch({ url: "/pages/fankui/fankui" });
					/*url = '../fankui/fankui';
					uni.navigateTo({
					  url: url
					});*/
				} else {
					uni.showToast({
						title: response.data.msg,
						icon: 'error',
						duration: 1000
					});
				}
			});
		},
		// 新增图片
		async afterRead(event) {
			var duixin = this; 
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			console.log('event',event); 
			let lists = [].concat(event.file)
			let fileListLen = this[`fileList${event.name}`].length
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url)
				const id = duixin.uptu.data.id;
				console.log('uptu',duixin.uptu.data.id); 
				let item = this[`fileList${event.name}`][fileListLen]
				this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result,
					id:id
				}))
				fileListLen++
				
			}
		},
		uploadFilePromise(url) {
			console.log('url',url); 
			var duixin = this; 
			var uid = duixin.uid;
			var keycode = duixin.keycode;
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					
					url: Api.postTupians(uid,keycode)+'&fid='+fankuitu, //
					filePath: url,
					name: 'file_data',
					formData: {
						is_ajax:1
					},
					success: (res) => {
						var duixin = this; 
						duixin.uptu = JSON.parse(res.data);
						setTimeout(() => {
							resolve(res.data)
						}, 1000)
						
					}
				});
			})
		}
		
		
    },
	computed: {
		
	},

};
</script>
<style>

page {
	background-color: #ffffff;
}
/* #ifdef H5 */
	::v-deep.uni-textarea-textarea {
	    resize: none;
	    background: none;
	    color: inherit;
	    opacity: 1;
	    font: inherit;
	    /* line-height: inherit; */
	    letter-spacing: inherit;
	    text-align: inherit;
	    text-indent: inherit;
	    text-transform: inherit;
	    text-shadow: inherit;
	    font-size: 18px;
	    line-height: 30px;
	}
	/* #endif */
	.uni-textarea-textarea {
	    resize: none;
	    background: none;
	    color: inherit;
	    opacity: 1;
	    font: inherit;
	    /* line-height: inherit; */
	    letter-spacing: inherit;
	    text-align: inherit;
	    text-indent: inherit;
	    text-transform: inherit;
	    text-shadow: inherit;
	    font-size: 18px;
	    line-height: 30px;
	}
	.place-holder {
		color:#b8b8b8;font-size:32rpx;text-align:center;
	}
</style>